package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.AddressBook;
import com.itheima.entity.OrderDetail;
import com.itheima.entity.Orders;
import com.itheima.entity.User;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @PostMapping("/submit")
    public R<String>submit(@RequestBody Orders orders){

        orderService.submit(orders);
        return R.success("下单成功");
    }


    /*
     * 订单明细
     *
     * */
    @GetMapping("/page")
    public R<Page>page(int page, int pageSize, Long number, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,@DateTimeFormat(pattern ="yyyy-MM-dd HH:mm:ss" ) LocalDateTime endTime){
        Page<Orders>PageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Orders> queryWrapper1 = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper1.eq(number != null,Orders::getNumber,number);
        queryWrapper1.ge(beginTime!=null,Orders::getOrderTime,beginTime);
        queryWrapper1.le(endTime!= null , Orders::getOrderTime,endTime);
        orderService.page(PageInfo,queryWrapper1);
        return R.success(PageInfo);

    }



@GetMapping("/userPage")
    public R<Page> userpage(Integer page,Integer pageSize){
        //分页构造器
        Page<Orders>page1 = new Page<>(page,pageSize);
        //查询当前用户ID的订单，根据下单时间降序排序。，调用orderService查询
        LambdaQueryWrapper<Orders>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        orderService.page(page1,queryWrapper);
        //创建构造器
        Page<OrdersDto> dtoPage = new Page<>();
        //把之前的拷贝过来，除了records
        BeanUtils.copyProperties(page1,dtoPage,"records");
        //获取records集合
        List<Orders> records = page1.getRecords();
        //records调用stream流，把records拷贝到ordersDto中
        List<OrdersDto> collect = records.stream().map((item) -> {
        OrdersDto ordersDto = new OrdersDto();
        BeanUtils.copyProperties(item, ordersDto);
        Long id = item.getId();

        LambdaQueryWrapper<OrderDetail> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(OrderDetail::getOrderId, id);
        List<OrderDetail> orderDetailList = orderDetailService.list(queryWrapper1);
        ordersDto.setOrderDetails(orderDetailList);
        return ordersDto;
    }).collect(Collectors.toList());

        dtoPage.setRecords(collect);

        return R.success(dtoPage);
}

}
